package com.park.controller;

import com.park.bean.Attracts;
import com.park.bean.AttractsType;
import com.park.common.CommonResult;
import com.park.config.WebConfig;
import com.park.service.IAttractsService;
import com.park.util.Page;
import com.park.util.UploadFileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class AttractsController {
    @Autowired
    IAttractsService attractsService;

    //展示和查询数据
    @GetMapping("attracts")
    public CommonResult getAttracts(Attracts attracts, Page page){
        //System.out.println("getAttracts---------");
        System.out.println();
        List<Attracts> res=attractsService.getAttracts(attracts,page);
        int count=attractsService.getCount(attracts);
        HashMap map=new HashMap();
        map.put("res",res);
        map.put("count",count);
        //System.out.println(res);
        return CommonResult.success(map);
    }

    @GetMapping("attracts/type")
    public CommonResult getAttractsType(){
        List<AttractsType> list=attractsService.getAttractsType();
        return CommonResult.success(list);
    }

    //添加数据
    @PostMapping("attracts")
    public CommonResult postAttracts(@RequestBody Attracts attracts){
        int count=attractsService.postAttracts(attracts);
        if(count>0){
            return CommonResult.success(attracts);
        }else{
            return CommonResult.fail();
        }
    }

    //删除数据
    @DeleteMapping("attracts/{id}")
    public CommonResult delAttracts(@PathVariable int id){
        int count= attractsService.delAttracts(id);
        if(count>0){
            return CommonResult.success();
        }else{
            return CommonResult.fail();
        }
    }

    //修改数据
    @PutMapping("attracts")
    public CommonResult editAttracts(@RequestBody Attracts attracts){
        int count=attractsService.editAttracts(attracts);
        if(count>0){
            return CommonResult.success(attracts);
        }else{
            return CommonResult.fail();
        }
    }

    //上传图片
    @PostMapping("uploadimg")
    public CommonResult uploadImg(@RequestParam("file") MultipartFile file){
        //存储文件
        String newName= UploadFileUtil.uploadfile(file);
        //将文件地址存入数据库
        //返回上传文件的地址 or 返回上传图片的设施数据
        return CommonResult.success(WebConfig.IMAGE_PATH+newName);
    }

    @PutMapping("attractsimg")
    public CommonResult storeImg(@RequestBody Attracts attracts){
        System.out.println("getdescription!!!!"+attracts.getDescription());
        System.out.println(attracts.getId());
        System.out.println(attracts.getName());
        int count=attractsService.editAttracts(attracts);
        if(count>0){
            return CommonResult.success(attracts);
        }else{
            return CommonResult.fail();
        }
    }
}
